<?php

/*
 * 订单类
 */

namespace Pay\Logic;

class OrderLogic {
    
    public function __construct() {
        $this->o = new \Home\Model\Action_orderModel();
        $this->mt = D('Marketing_team');
    }
    
    public function createOrder($data,$user_id,$team_number=false){
        $this->o->startTrans();
        $data2['order_nmuber'] = $team_number==false?$this->createNumber():$team_number;
        $data2['user_id'] = $user_id;
        $data2['type'] = $team_number==false?1:0;
        $id = $this->createTeam($data2);
        if($id<1){
            $this->o->rollback();
            return false;
        }
        $id = $this->o->add($data);
        if($id<1){
            $this->o->rollback();
            return false;
        }
        $this->o->commit();
        return $data['order_number'];
    }
    
    public function createNumber(){
        return uniqid('ID',true);
    }
    
    public function updateOrder($order_no,$data){
        $re = $this->o->where(array('order_number'=>$order_no))->save($data);
        if($re!==false){
            return true;
        }
        return false;
    }
    
    public function createTeam($data){
        $data['create_time'] = time();
        return $this->mt->add($data);
    }
    
    public function getAction($id){
        $a = new \Home\Model\ActionModel();
        return $a->where(array('id'=>$id))->find();
    }
    
    public function getOrder($order_number){
        return $this->o->where(array('order_number'=>$order_number))->find();
    }
    
}
